<?php

/**
 * @file classes/manager/form/DMTKForm.inc.php
 *
 * Copyright (c) 2003-2012 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @class DMTKForm
 * @ingroup manager_form
 *
 * @brief Form for creating and modifying journal DMTKs.
 */

// $Id$


import('lib.pkp.classes.form.Form');

class DMTKForm extends Form {

	/** @var $DMTKId int The ID of the DMTK being edited */
	var $DMTKId;

	/** @var $includeDMTKEditor object Additional DMTK editor to
	 *       include in assigned list for this DMTK
	 */
	var $includeDMTKEditor;

	/** @var $omitDMTKEditor object Assigned DMTK editor to omit from
	 *       assigned list for this DMTK
	 */
	var $omitDMTKEditor;

	/** @var $DMTKEditors array List of user objects representing the
	 *       available DMTK editors for this journal.
	 */
	var $DMTKEditors;

	/**
	 * Constructor.
	 * @param $journalId int omit for a new journal
	 */
	function DMTKForm($DMTKId = null) {
		parent::Form('acctKT/DMTKForm.tpl');

		$journal =& Request::getJournal();
		$this->DMTKId = $DMTKId;

		// Validation checks for this form
		$this->addCheck(new FormValidatorLocale($this, 'matk', 'required', 'Phai co Ma TK!'));
		$this->addCheck(new FormValidatorLocale($this, 'tentk', 'required', 'Phai co Ten TK'));
		$this->addCheck(new FormValidatorPost($this));
		

		// Get a list of DMTK editors for this journal.
		$roleDao =& DAORegistry::getDAO('RoleDAO');
		$this->DMTKEditors =& $roleDao->getUsersByRoleId(ROLE_ID_DMTK_EDITOR, $journal->getId());
		$this->DMTKEditors =& $this->DMTKEditors->toArray();
		
	}

	/**
	 * When displaying the form, include the specified DMTK editor
	 * in the assigned list for this DMTK.
	 * @param $DMTKEditorId int
	 */
	function includeDMTKEditor($DMTKEditorId) {
		foreach ($this->DMTKEditors as $key => $junk) {
			if ($this->DMTKEditors[$key]->getId() == $DMTKEditorId) {
				$this->includeDMTKEditor =& $this->DMTKEditors[$key];
			}
		}
	}

	/**
	 * When displaying the form, omit the specified DMTK editor from
	 * the assigned list for this DMTK.
	 */
	function omitDMTKEditor($DMTKEditorId) {
		foreach ($this->DMTKEditors as $key => $junk) {
			if ($this->DMTKEditors[$key]->getId() == $DMTKEditorId) {
				$this->omitDMTKEditor =& $this->DMTKEditors[$key];
			}
		}
	}

	/**
	 * Get the names of fields for which localized data is allowed.
	 * @return array
	 */
	function getLocaleFieldNames() {
		$DMTKDao =& DAORegistry::getDAO('DMTKDAO');
		return $DMTKDao->getLocaleFieldNames();
	}

	/**
	 * Display the form.
	 */
	function display() {
		$journal =& Request::getJournal();
		$templateMgr =& TemplateManager::getManager();

		$templateMgr->assign('DMTKId', $this->DMTKId);
		
		parent::display();
	}

	/**
	 * Initialize form data from current settings.
	 */
	function initData() {
		$journal =& Request::getJournal();
		if (isset($this->DMTKId)) {
			$DMTKDao =& DAORegistry::getDAO('DMTKDAO');
			$DMTK =& $DMTKDao->getDMTK($this->DMTKId, false, false);
			if ($DMTK == null) {
				unset($this->DMTKId);
			} else {
				$this->_data = array(
					'matk' => $DMTK->getmatk(), // Localized
					'tentk' => $DMTK->gettentk(), // Localized
				);
			}
		}
	}

	/**
	 * Assign form data to user-submitted data.
	 */
	function readInputData() {
		$this->readUserVars(array('matk', 'tentk'));
	}

	/**
	 * Save DMTK.
	 */
	function execute() {

		$DMTKDao =& DAORegistry::getDAO('DMTKDAO');

		if (isset($this->DMTKId)) {
			$DMTK =& $DMTKDao->getDMTK($this->DMTKId, false, false);
		}

		if (!isset($DMTK)) {
			$DMTK = new DMTK();
		}

		$DMTK->setmatk($this->getData('matk'), null); // Localized
		$DMTK->settentk($this->getData('tentk'), null); // Localized

		
		if (isset($this->DMTKId)) {
			$DMTKDao->updateDMTK($DMTK);
			$DMTKId = $DMTK->getId();

		} else {

			$DMTKId = $DMTKDao->insertDMTK($DMTK);
			
		}

	}
}

?>
